package cn.com.server.mongo.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

@Repository
public class MongoDBDaoImpl implements MongoDBDao{
    
    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Override
    public <T> T findOne(Class<T> clazz, Query query) {
        return mongoTemplate.findOne(query, clazz);
    }

    @Override
    public void save(Object obj) {
        mongoTemplate.save(obj);
    }

	@Override
	public void insert(Object obj) {
		mongoTemplate.insert(obj);
	}

    @Override
    public <T> List<T> findAll(Class<T> clazz) {
        return mongoTemplate.findAll(clazz);
    }

    @Override
    public <T> T findById(Class<T> clazz, Object id) {
        return mongoTemplate.findById(id, clazz);
    }

    @Override
    public <T> List<T> find(Class<T> clazz, Query query) {
        return mongoTemplate.find(query, clazz);
    }

    @Override
    public <T> List<T> findList(Class<T> clazz, Query query, int currentPage,
            int pageSize) {
        //计算起始位置
        int startIndex = ((currentPage - 1)<0?0:(currentPage - 1))*pageSize;
        query.skip(startIndex);
        query.limit(pageSize);
        return mongoTemplate.find(query,clazz);
    }

    @Override
    public <T> long findCount(Class<T> clazz, Query query) {
        return mongoTemplate.count(query, clazz);
    }

    @Override
    public <T> int update(Query query, Update update, Class<T> clazz) {
        return mongoTemplate.updateFirst(query, update, clazz).getN();
    }

	@Override
	public <T> boolean remove(Object object) {
		return mongoTemplate.remove(object).isUpdateOfExisting();
	}

	@Override
	public <T> int remove(Query query, Class<T> clazz) {
		return mongoTemplate.remove(query, clazz).getN();
	}

	@Override
	public String getId(String seq_name) {
        String sequence_collection = "seq"; // the name of the sequence collection
        String sequence_field = "seq"; // the name of the field which holds the sequence

        DBCollection seq = mongoTemplate.getCollection(sequence_collection); // get the collection (this will create it if needed)

        // this object represents your "query", its analogous to a WHERE clause in SQL
        DBObject query = new BasicDBObject();
        query.put("_id", seq_name); // where _id = the input sequence name

        // this object represents the "update" or the SET blah=blah in SQL
        DBObject change = new BasicDBObject(sequence_field, 1);
        DBObject update = new BasicDBObject("$inc", change); // the $inc here is a mongodb command for increment

        // Atomically updates the sequence field and returns the value for you
        DBObject res = seq.findAndModify(query, new BasicDBObject(), new BasicDBObject(), false, update, true, true);
        return res.get(sequence_field).toString();
	}
}
